<?php
namespace application\system\service;
/**
 * Description of UserService
 *
 * @author Faraj
 */
class AuthService extends \simbola\core\application\AppService{
     
    public $schema_login = array(
        'req' => array('params' => array('username','password')),
        'res' => array(),
        'err' => array('LOGIN_FAILED'),
    );

    function actionLogin() {        
        $skey = \simbola\Simbola::app()->auth->login(
                $this->_req_params("username"),
                $this->_req_params("password"));
        if($skey){            
            $this->_res('username', $this->_req_params("username"));
            $this->_res('skey', $skey);
        }else{
            $this->_err("LOGIN_FAILED");
        }
    }
    
    public $schema_logout = array(
        'req' => array('params' => array()),
        'res' => array(),
        'err' => array(),
    );

    function actionLogout() {
        \simbola\Simbola::app()->auth->logout();
    }        
    
    // User --------------------------------------------------------------------
    public $schema_userView = array(
        'req' => array('params' => array('keys')),
        'res' => array('object'),
        'err' => array('VIEW_ERROR')
    );

    function actionUserView() {
        $object = \application\system\model\auth\User::find($this->_req_params('keys'));        
        if($object == NULL){
            $this->_err('VIEW_ERROR');
        }
        $object->user_password = null; //for security reasons
        $this->_res('object', $object);
    }
    
    public $schema_userList = array(
        'req' => array('params' => array()),
        'res' => array('data'),
        'err' => array('LIST_ERROR')
    );

    function actionUserList() {
        try {
            $search = $this->_req_params('search');
            if(empty($search)){
               $data = \application\system\model\auth\User::find('all');   
            }else{
               $data = \application\system\model\auth\User::find('all', $search);  
            }            
            for ($index = 0; $index < count($data); $index++) {
                $data[$index]->user_password = null;
            }
            $this->_res('data', $data);
        } catch(Exception $ex) {
            $this->_err('LIST_ERROR');
        }
    }
    
    public $schema_userCreate = array(
        'req' => array('params' => array('data')),
        'res' => array('object'),
        'err' => array('CREATE_ERROR')
    );

    function actionUserCreate() {
        $object = new \application\system\model\auth\User($this->_req_params('data'));        
        if(!$object->save()){
            $this->_err('CREATE_ERROR');
        }
        $this->_res('object', $object);
    }
    
    public $schema_userUpdate = array(
        'req' => array('params' => array('keys', 'data')),
        'res' => array('object'),
        'err' => array('UPDATE_ERROR')
    );

    function actionUserUpdate() {
        $object = \application\system\model\auth\User::find($this->_req_params('keys'));        
        $object->set_attributes($this->_req_params('data'));
        if(!$object->save()){
            $this->_err('UPDATE_ERROR');
        }
        $this->_res('object', $object);
    }
    
    public $schema_userDelete = array(
        'req' => array('params' => array('keys')),
        'res' => array(),
        'err' => array('DELETE_ERROR')
    );

    function actionUserDelete() {
        $object = \application\system\model\auth\User::find($this->_req_params('keys'));
        if(!$object->delete()){
            $this->_err('DELETE_ERROR');
        }        
    }
    
    // Role --------------------------------------------------------------------
    public $schema_roleView = array(
        'req' => array('params' => array('keys')),
        'res' => array('object'),
        'err' => array('VIEW_ERROR')
    );

    function actionRoleView() {
        $object = \application\system\model\auth\Role::find($this->_req_params('keys'));        
        if($object == NULL){
            $this->_err('VIEW_ERROR');
        }
        $this->_res('object', $object);
    }
    
    public $schema_roleList = array(
        'req' => array('params' => array()),
        'res' => array('data'),
        'err' => array('LIST_ERROR')
    );

    function actionRoleList() {
        try {
            $search = $this->_req_params('search');
            if(empty($search)){
               $data = \application\system\model\auth\Role::find('all');   
            }else{
               $data = \application\system\model\auth\Role::find('all', $search);  
            }
            $this->_res('data', $data);            
        } catch(Exception $ex) {
            $this->_err('LIST_ERROR');
        }
    }
    
    public $schema_roleCreate = array(
        'req' => array('params' => array('data')),
        'res' => array('object'),
        'err' => array('CREATE_ERROR')
    );

    function actionRoleCreate() {
        $object = new \application\system\model\auth\Role($this->_req_params('data'));        
        if(!$object->save()){
            $this->_err('CREATE_ERROR');
        }
        $this->_res('object', $object);
    }
    
    public $schema_roleUpdate = array(
        'req' => array('params' => array('keys', 'data')),
        'res' => array('object'),
        'err' => array('UPDATE_ERROR')
    );

    function actionRoleUpdate() {
        $object = \application\system\model\auth\Role::find($this->_req_params('keys'));        
        $object->set_attributes($this->_req_params('data'));
        if(!$object->save()){
            $this->_err('UPDATE_ERROR');
        }
        $this->_res('object', $object);
    }
    
    public $schema_roleDelete = array(
        'req' => array('params' => array('keys')),
        'res' => array(),
        'err' => array('DELETE_ERROR')
    );

    function actionRoleDelete() {
        $object = \application\system\model\auth\Role::find($this->_req_params('keys'));
        if(!$object->delete()){
            $this->_err('DELETE_ERROR');
        }        
    }
    
    public $schema_roleAssign = array(
        'req' => array('params' => array('keys')),
        'res' => array(),
        'err' => array('ASSIGN_ERROR', 'ALREADY_ASSIGNED')
    );

    function actionRoleAssign() {
        $roleAssign = \application\system\model\auth\RoleAssign::find($this->_req_params('keys'));
        if($roleAssign != null){
            $this->_err('ALREADY_ASSIGNED');
        }else{
            $roleAssign = new \application\system\model\auth\RoleAssign($this->_req_params('keys'));
            if(!$roleAssign->save()){
                $this->_err('ASSIGN_ERROR');
            }
        }
    }
    
    public $schema_roleRevoke = array(
        'req' => array('params' => array('keys')),
        'res' => array(),
        'err' => array('REVOKE_ERROR', 'NOT_ASSIGNED')
    );

    function actionRoleRevoke() {
        $roleAssign = \application\system\model\auth\RoleAssign::find($this->_req_params('keys'));
        if($roleAssign == null){
            $this->_err('NOT_ASSIGNED');
        }else{
            if(!$roleAssign->delete()){
                $this->_err('REVOKE_ERROR');
            }
        }
    }
}

